package com.example.finaltool.model.config;

import com.example.finaltool.consts.key.DataKey;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dengbw
 * @date 2021/8/11 11:20 上午
 */
@Getter
public class Data {
    private String corpId;
    private String ssosecret;
    private String agentId;
    private String appKey;
    private String appSecret;
    private String shi;
    private String quxian;
    private String zhen;
    private final List<Zhudiwang> zhudiwang = new ArrayList<>();

    public Map<String, Map<String, String>> getBianhaoAndId() {
        Map<String, Map<String, String>> res = new HashMap<>(zhudiwang.size());
        if (CollectionUtils.isEmpty(zhudiwang)) {
            return res;
        }
        zhudiwang.forEach(z -> {
            Map<String, String> infoMap = new HashMap<>(2);
            infoMap.put(Zhudiwang.KEY_BIANHAO, z.getBianhao());
            infoMap.put(Zhudiwang.KEY_ID, z.getId());
            res.put(z.getCun(), infoMap);
        });
        return res;
    }

    public void put(String key, String value) {
        switch (key) {
            case DataKey.CORPID:
                corpId = value;
                break;
            case DataKey.SSOSECRET:
                ssosecret = value;
                break;
            case DataKey.AGENTID:
                agentId = value;
                break;
            case DataKey.APPKEY:
                appKey = value;
                break;
            case DataKey.APPSECRET:
                appSecret = value;
                break;
            case DataKey.SHI:
                shi = value;
                break;
            case DataKey.QUXIAN:
                quxian = value;
                break;
            case DataKey.ZHEN:
                zhen = value;
                break;
            case DataKey.ZHUDIWANG:
                if (StringUtils.isNotBlank(value)) {
                    String[] s = value.split(",");
                    assert s.length == 3 : "驻地网配置错误";
                    zhudiwang.add(new Zhudiwang(
                            s[0].replace(" ", ""),
                            s[1].replace(" ", ""),
                            s[2].replace(" ", "")));
                }
                break;
            default:
        }
    }

    public String getString(String key) {
        switch (key) {
            case DataKey.CORPID:
                return corpId;
            case DataKey.SSOSECRET:
                return ssosecret;
            case DataKey.AGENTID:
                return agentId;
            case DataKey.APPKEY:
                return appKey;
            case DataKey.APPSECRET:
                return appSecret;
            case DataKey.SHI:
                return shi;
            case DataKey.QUXIAN:
                return quxian;
            case DataKey.ZHEN:
                return zhen;
            default:
                return "";
        }
    }

    public boolean contains(String key) {
        if (DataKey.ZHUDIWANG.equals(key)) {
            return !CollectionUtils.isEmpty(zhudiwang);
        } else {
            return StringUtils.isNotBlank(getString(key));
        }
    }
}
